#include "httppostprobe.h"
#include "ui_httppostprobe.h"

//----------------------------------------------------------------
httpPostProbe::httpPostProbe(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::httpPostProbe)
{
    ui->setupUi(this);

    urlLineEdit=0;
    argLineEdit=0;
    retTextEdit=0;
    pathLineEdit=0;

    if (advSetupUi()>=0)
        {
            urlLineEdit->setText("http://127.0.0.1");
            pathLineEdit->setText("/prpost.php");
            argLineEdit->setText("postparam=value&postParam1=value2");
        };

    httpObj = new t_http(this);
    connect(httpObj, SIGNAL(replyFinished()),
                this, SLOT(replyFinished()));
}

//----------------------------------------------------------------
int httpPostProbe::advSetupUi()
{
    QObject *someObject;
    someObject  = qFindChild<QObject *> (this, "urlLineEdit"); //
    urlLineEdit = (QLineEdit *)someObject;
    if (urlLineEdit == NULL)
     {  qDebug() << "httpPostProbe([00018] Can`t find [urlLineEdit] at aggregated ui-widget.";   return -1;
     };

    someObject  = qFindChild<QObject *> (this, "argLineEdit"); //
    argLineEdit = (QLineEdit *)someObject;
    if (argLineEdit == NULL)
     {  qDebug() << "httpPostProbe([00018] Can`t find [argLineEdit] at aggregated ui-widget.";   return -1;
     };

    someObject  = qFindChild<QObject *> (this, "retTextEdit"); //
    retTextEdit = (QPlainTextEdit *)someObject;
    if (retTextEdit == NULL)
     {  qDebug() << "httpPostProbe([00018] Can`t find [retTextEdit] at aggregated ui-widget.";   return -1;
     };

    someObject  = qFindChild<QObject *> (this, "pathLineEdit"); //
    pathLineEdit = (QLineEdit *)someObject;
    if (pathLineEdit == NULL)
     {  qDebug() << "httpPostProbe([00018] Can`t find [pathLineEdit] at aggregated ui-widget.";   return -1;
     };

    return 0;
}

//----------------------------------------------------------------
httpPostProbe::~httpPostProbe()
{
    delete ui;
    delete httpObj;
}

//----------------------------------------------------------------
void httpPostProbe::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//----------------------------------------------------------------
void httpPostProbe::on_pushButton_clicked(bool checked)
{
    QString cKey,cValue;

    httpObj->setHost(urlLineEdit->text());
    httpObj->setPath(pathLineEdit->text());
    //httpObj->setHostAndPath(urlLineEdit->text());

    QStringList argList=argLineEdit->text().split("&");
    foreach (QString part, argList)
        {
            cKey=part.left(part.indexOf("="));
            cValue=part.mid(part.indexOf("=")+1);
            httpObj->setArgument(cKey,cValue);
        };

    httpObj->post();

    if (!httpObj->waitForReplyFinished(5000))
        {
            QString errStr=QString("%1 %2").arg(httpObj->error()).arg(httpObj->errorString());
            retTextEdit->setPlainText("ERROR: Timeout!");
            retTextEdit->appendPlainText(errStr);
            return;
        };
    if (httpObj->error()!=0)
        {
            QString errStr=QString("ERROR: %1 %2").arg(httpObj->error()).arg(httpObj->errorString());
            retTextEdit->appendPlainText(errStr);
            return;
        };

    retTextEdit->setPlainText(httpObj->getLastReplyText());
    return;

}

//----------------------------------------------------------------
void httpPostProbe::replyFinished()
{
    QByteArray replyText;
    qDebug() << "httpObj->getReply()->bytesAvailable() = " << httpObj->getReply()->bytesAvailable();
    replyText=httpObj->getLastReplyText();
    //retTextEdit->setPlainText(QString(replyText));
    retTextEdit->appendPlainText(QString(replyText));
}

//----------------------------------------------------------------
void httpPostProbe::slotError()
{
    QString errStr=QString("%1 %2").arg(httpObj->error()).arg(httpObj->errorString());
    retTextEdit->appendPlainText(errStr);
}

//----------------------------------------------------------------
